import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.text.*;
import java.util.*;

public class LeftBarServlet extends HttpServlet {

    private static RandomAccessFile outputFile;
    private static Hashtable table = new Hashtable ();

    private static String baseDir    = null;
    private static String systemDir  = null;
    private static String htmlDir    = null;
    private static String jspDir     = null;

    public void doPost(HttpServletRequest request,
                       HttpServletResponse response)
        throws IOException, ServletException
    {
	response.setContentType("text/html");
        PrintWriter out = response.getWriter();

	GenerateDirectoryList maker = new GenerateDirectoryList ();
	maker.setDirPath ("C:/ApacheGroup/Apache2/htdocs");
	maker.generateHtmlLeftBar();

        out.println("<html>");
        out.println("<head>");
	out.println("<title>Stock chart index</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Stock Chart Index</h1>");
        out.println("</body>");
        out.println("</html>");
    }

    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
        throws IOException, ServletException
    {
	doPost(request, response);
    }

    private void createLeftBarHtmlFile()
    {

	File file_name = new File (htmlDir, "index_left.html");

	try {
	    if (file_name.exists()) {
		System.out.println("deleting old index_left.html file .....");
		if(file_name.delete()) {
		    System.out.println("Old index_left.html file has been deleted");
		}
	    }
	    outputFile = new RandomAccessFile( file_name, "rw" );

	}
	catch ( IOException e ) {
	}
    }

    private void loadLeftBarProfile() {

	File check_profile = new File (baseDir, "_profile.txt");

	if (check_profile.exists()) {
	    SymbolFileLoader loader = new SymbolFileLoader();
	    loader.setFileName(baseDir+"/_profile.txt");
	    loader.loadUrlProfile();
	    table = loader.getHashTable();
	}
	else {
	    System.out.println("No profile for this directory");
	}
    }

    public void addHtmlHeadToLeftBar( )
    {
       try {
	   outputFile.writeBytes("<HTML><HEAD><TITLE>Direcotry List</TITLE></HEAD>");
	   outputFile.writeBytes("<BODY bgcolor=\"#808080\"><TABLE border='0' width='100' height='100'>");
	   outputFile.writeBytes("<TR><TD><IMG height ='100' width='100' SRC='/_system/images/3wc.JPG'>");
	   outputFile.writeBytes("</TD></TR></TABLE><BR>");
       }
       catch ( IOException io ) {
       }
    }

    public void addHtmlProfileToLeftBar()
    {

	/**
	 * add user profile into side bar
	 **/

	Hashtable profile_hs = new Hashtable ();
	String    url_name   = null;
	String    url_adds   = null;
	int       i          = 1;

	try {
	
	    Enumeration enum = table.keys();  
	    while (enum.hasMoreElements()) {
		url_name = (String)enum.nextElement();
		url_adds = (String)table.get(url_name);
		if(!url_name.startsWith("#")) {
		    outputFile.writeBytes("<A href ='"+url_adds+"'>"+url_name.toUpperCase()+"</A><BR>");
		}
	    }
	}
	catch ( IOException io ) {
	}
    }
	
    public void addHtmlBodyToLeftBar()
    {
	/**
	 * add directories list into side bar
	 **/

	String   sub_dir       = null;
	String[] file_list     = null;

	file_list = getDirList(baseDir);

	for (int n = 0; n < file_list.length; n++) {

	    File f_file_list = new File (baseDir, file_list[n]);
	    
	    if (f_file_list.isDirectory()) {
		sub_dir = file_list[n];
		System.out.println("subDir="+sub_dir);

		if(!sub_dir.startsWith("_")) {

		    /**
		     * add sub directory name into side bar
		     */

		    try {
			outputFile.writeBytes("<A href='./../../"+sub_dir+"/_system/html/index_left.html' target='leftFrame'>");
			outputFile.writeBytes(sub_dir.toUpperCase()+"</A><BR>");
		    }
		    catch ( IOException io ) {
		    }
		}
	    }
	}
    }

    public void addHtmlEnd( )
    {
	try {
	    outputFile.writeBytes("</BODY></HTML>");
	}
	catch ( IOException io ) {
	}
    }

    private void closeFile(RandomAccessFile raf) 
    {
	try {
	    raf.close();
	}
	catch( IOException ex ) {
	    System.exit( 1 );
	}
    }

    public String[] getDirList(String dir)
    {
	File dirName = new File (dir);
	return(dirName.list());
    }

    public static void main( String args[] )
    {
	

	if (args.length != 1) {
	    System.out.println("========================================");
	    System.out.println("Please enter the full path of directory");
	    System.out.println("========================================");
	    System.exit(0);
	}
	else {
	    baseDir = args[0];
	    File f_dir = new File (baseDir);
	    if (!f_dir.isDirectory()) {
		System.out.println(baseDir+" is not a directory!");
		System.exit(0);
	    }
	}

	LeftBarServlet maker = new LeftBarServlet();

	systemDir  = baseDir + "/_system/";
	htmlDir    = systemDir + "html/";
	jspDir     = systemDir + "javaScript/";

	File dir = new File (baseDir);
	//dir.mkdir();
	
	if(dir.isDirectory()) {
	    System.out.println("Directory:"+dir.getName()+" found!");
	}
	else {
	    System.out.println("Directory:"+dir.getName()+" not found!");
	    System.exit(0);
	}
	
	maker.checkSystemDirectory(systemDir);
	maker.checkSystemDirectory(htmlDir);
	maker.checkSystemDirectory(jspDir);

	//System.exit(0);

	maker.createLeftBarHtmlFile();
	maker.loadLeftBarProfile();
	maker.addHtmlHeadToLeftBar();
	maker.addHtmlProfileToLeftBar();
	maker.addHtmlBodyToLeftBar();
	maker.addHtmlEnd();
	maker.closeFile(outputFile);
    }

    private void checkSystemDirectory (String sub_dir) {
	File sub_d_f = new File(sub_dir);
	if(sub_d_f.isDirectory()) {
	    System.out.println("Directory:"+sub_d_f.getName()+" found!");
	}
	else {
	    System.out.println("Directory:"+sub_d_f.getName()+" not found!");
	    sub_d_f.mkdir();
	    System.out.println("Directory:"+sub_d_f.getName()+" has been created!");
	}
    }
}



